package cn.fruitmarket.sellstrategy;

import cn.fruitmarket.utils.DateUtil;
import cn.fruitmarket.utils.MoneyUtil;

import java.util.Date;

public class RebateStrategy implements IStrategy {
    private  double rate ;
    private Date startTime;
    private Date endTime;
    public RebateStrategy(String startTimeStr,String endTimeStr,double rate) throws Exception{
        if(rate>1||rate<0) {
            throw new Exception("折扣必须大于0小于1");
        }
        this.startTime = DateUtil.parse(startTimeStr);
        this.endTime = DateUtil.parse(endTimeStr);
        this.rate = rate;
    }
    public double realPrice(Date carDate,double consumePrice) {

        long currDate = carDate.getTime();
        if(currDate>=startTime.getTime() && currDate <= endTime.getTime()) {
            return MoneyUtil.moneyMul(consumePrice,rate);
        }else {
            return consumePrice;
        }

    }
}
